function splitWord(str: string): string[] {
  return str.match(/[a-z]+[\-\']?[a-z]*/gi)!;
}

function findRepetitive(
  str: string[],
  target: string,
  sub: number,
  count: number
): number {
  if (sub < 0) {
    return count;
  }
  let subd = str.indexOf(target, sub);
  if (subd < 0) {
    return count;
  } else {
    return findRepetitive(str, target, (subd += 1), (count += 1));
  }
}
function deduplication(...arg: string[][]): number[][] {
  let sets: Set<string> = new Set<string>();
  let temp: string[] = [];
  arg.forEach((e) => {
    temp = temp.concat(e);
  });
  temp.forEach((e) => {
    sets.add(e);
  });
  temp = Array.from(sets);
  let arr = [];
  for (let i = 0; i < arg.length; i++) {
    let tempArr = new Array<number>(temp.length);
    for (let k = 0; k < arg[i].length; k++) {
      for (let j = 0; j < temp.length; j++) {
        let d = arg[i][k].indexOf(temp[j]);
        if (d > -1) {
          let count = findRepetitive(arg[i], temp[j], d, 0);
          tempArr[j] = count;
        }
      }
    }
    for (let s = 0; s < tempArr.length; s++) {
      if (tempArr[s] === undefined) {
        tempArr[s] = 0;
      }
    }
    arr.push(tempArr);
  }
  return arr;
}

function cos(nums: number[][]) {
  let result: number[] = [];
  let resultB: number = 0;

  for (let i = 0; i < nums[0].length; i++) {
    resultB += nums[0][i] * nums[1][i];
  }
  nums.forEach((e) => {
    let temp = 0;
    e.forEach((d) => {
      temp += Math.pow(d, 2);
      // console.log(d);
    });
    result.push(Math.sqrt(temp));
  });
  let sqrt = result[0] * result[1];
  return (resultB / sqrt) * 100;
}

function calVection(str1: string, str2: string): number {
  let v1 = splitWord(str1);
  let v2 = splitWord(str2);

  let t = deduplication(v1, v2);

  return cos(t);
}

// export default calVection;
let d = new Date().getTime();
let a = calVection(`I love you`, `I love you too`);
let s = Date.now();
console.log(d, s);
